<template>
  <div class="container">
    <div class="traces-wraper">
      <el-table :data="formatIndexData" 
      :header-cell-style="tableHeaderColor"
      :span-method="cellMerge1"
      style="width: 100%;" border>
        <el-table-column label="痕迹摘要" prop="abstract" min-width="150" align="left">
          <template slot-scope="scope">{{ scope.row.abstract}} </template>
        </el-table-column>
        <el-table-column label="痕迹材料" min-width="200" align="left">
          <template slot-scope="scope">
            <image-type v-if="scope.row.fileType" :fileType="scope.row.fileType">{{ scope.row.file | filenameFilter
              }}</image-type>
            <!-- <img :src="require('@/assets/images/'+ fileFilter(scope.row.fileType) +'.png')" alt="" class="file-img">{{ scope.row.file }} -->
          </template>
        </el-table-column>
        <el-table-column label="上传时间" min-width="100">
          <template slot-scope="scope">{{ parseTime(scope.row.time) }}</template>
        </el-table-column>
        <el-table-column label="上传用户" prop="user" width=""></el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { parseTime, getFileType } from "@/utils/index";
import { getIndexByid, getMarkInfo } from "@/api/work.js";
import ImageType from '@/components/ImageType'
export default {
  name: "IndexShow",
  components:{
      ImageType
    },
  props:{
    targetId:{
      type: Array,
      require: true
    }
  },
  watch: {
    targetId:{
      handler(result, oldName) {
        this.spanArr= [],
        this.pos = 0,
        this.getSpanArr(result)
        this.formatIndexData = result
      },
      immediate: true
    }
  },
  data() {
    return {
      spanArr: [],
      pos: 0,
      formatIndexData:[]
    };
  },
  // created() {
  //   this.fetchMarkInfo()
  // },
  methods: {
    parseTime,
    fetchMarkInfo() {
      getMarkInfo(this.targetId).then(res => {
        const data = res.data.results
        const result = this.flattenMark(data)
        this.getSpanArr(result)
        this.formatIndexData = result
      });
    },
    getSpanArr(data) {　
  for (var i = 0; i < data.length; i++) {
        if (i === 0) {
              this.spanArr.push(1);
              this.pos = 0
        } else {
          // 判断当前元素与上一个元素是否相同
    if (data[i].abstract === data[i - 1].abstract) {
                this.spanArr[this.pos] += 1;
                this.spanArr.push(0);
              } else {
                this.spanArr.push(1);
                this.pos = i;
              }
        }
    }
},
    cellMerge1({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 3) {
            const _row = this.spanArr[rowIndex];
            const _col = _row > 0 ? 1 : 0;
            return {
                  rowspan: _row,
                  colspan: _col
            }
      }
},
    
    cellMerge({ row, column, rowIndex, columnIndex }) {
      const span = column["property"] + "-span";
      if (row[span]) {
        return row[span];
      }
    },
    mergeTableRow(data, merge) {
      if (!merge || merge.length === 0) {
        return data;
      }
      merge.forEach(m => {
        const mList = {};
        data = data.map((v, index) => {
          const rowVal = v[m];
          if (mList[rowVal] && mList[rowVal].newIndex === index) {
            mList[rowVal]["num"]++;
            mList[rowVal]["newIndex"]++;
            data[mList[rowVal]["index"]][m + "-span"].rowspan++;
            v[m + "-span"] = {
              rowspan: 0,
              colspan: 0
            };
          } else {
            mList[rowVal] = { num: 1, index: index, newIndex: index + 1 };
            v[m + "-span"] = {
              rowspan: 1,
              colspan: 1
            };
          }
          return v;
        });
      });
      return data;
    },
    // 修改table header的背景色
    tableHeaderColor({ row, rowIndex }) {
    return 'background-color: #f5f5f5;color: #666;'
    },
  }
};
</script>
<style lang="scss">

</style>
